package com.winter.plugin;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowAnchor;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.psi.PsiFile;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import com.winter.plugin.vo.DataVo;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;


public class HelloWorldAction extends AnAction {

    private JPanel jcontent;
    private JScrollPane scrollPane;

    @Override
    public void actionPerformed(AnActionEvent e) {
        Project project = e.getData(PlatformDataKeys.PROJECT);

        PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
        String classPath = psiFile.getVirtualFile().getPath();
        String title = "Hello World!";

        jcontent = new JPanel(new GridBagLayout());
        jcontent.setBorder(BorderFactory.createLineBorder(Color.red, 1));
        scrollPane = new JScrollPane(jcontent);
        for (int i = 0; i < 5; i++) {
            DataVo item = new DataVo();
            item.setAuthor("lwx616851");
            item.setId("edafedadsadfasdfsadsafa" + String.valueOf(i));
            item.setDate("2020-01-21 09:00:00");
            item.setMessage("lwxidfasdfasdfaaaaaaa4ojrepsrjpejrjldsjgldsjgdslgrfpsdrfp616851");
            ListPanel listPanel = new ListPanel(item);
            listPanel.setBorder(BorderFactory.createLineBorder(Color.GREEN, 1));

            jcontent.add(listPanel, new GridBagConstraints(0, i, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
        }


        ToolWindowManager twm = ToolWindowManager.getInstance(e.getProject());
        ToolWindow cipherToolWindow = twm.registerToolWindow("codegraph", false, ToolWindowAnchor.RIGHT);

        ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
        Content content = contentFactory.createContent(scrollPane, "", false);
        cipherToolWindow.getContentManager().addContent(content);
        cipherToolWindow.activate(null);
    }
}
